<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <script>
    const arr = [8, 9, 1, 7, 2, 3, 5, 4, 6, 0];

    // 冒泡排序  每一轮都找出最大的那个数放到最后面
    function bubbleSort(arr) {
      const len = arr.length;
      for (let i = 0; i < len - 1; i++) {
        for (let j = 0; j < len - 1 - i; j++) {
          if (arr[j] > arr[j + 1]) {
            const swap = arr[j + 1];
            arr[j + 1] = arr[j];
            arr[j] = swap;
          }
        }
      }
      return arr;
    }

    console.log("冒泡排序----->", bubbleSort(arr));

    // 选择排序 选择出来一个最小的值放到最前面

    function selectSort(arr) {
      const len = arr.length;
      for (let i in arr) {
        let minIndex, temp;
        for (let j = i + 1; j < len; j++) {
          if (arr[j] > arr[j + 1]) {
            minIndex = j + 1;
          }
        }
        temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;
      }

      return arr;
    }

    console.log("选择排序----->", bubbleSort(arr));

    // 插入排序 向前扫描已排序的序列
    function insertSort(arr) {
      let len = arr.length;
      let preIndex, current;
      for (var i = 1; i < len; i++) {
        let current = arr[i];
        preIndex = i - 1;
        while (preIndex >= 0 && arr[preIndex] > current) {
          //不断将大于当前考察对象的数后移一位，直到找到考察对象应该处于的位置。
          arr[preIndex + 1] = arr[preIndex];
          preIndex--;
        }
        arr[preIndex + 1] = current;
      }
      return arr;
    }

    console.log("插入排序——-------->>", insertSort(arr));

    // 希尔排序 希尔排序里面包括了插入排序
    // https://www.cnblogs.com/lidedong/p/9780259.html 分组 结合插入排序的算法

    function shellSort(arr) {
      let len = arr.length;

      for (
        let gap = Math.floor(len / 2); gap > 0; gap = Math.floor(gap / 2)
      ) {
        for (let i = gap; i < len; i++) {
          let j = i;
          const current = arr[i];
          while (j - gap >= 0 && current < arr[j - gap]) {
            arr[j] = arr[j - gap];
            j = j - gap;
          }
          arr[j] = current;
        }
      }
      return arr;
    }
    console.log("希尔排序——-------->>", shellSort(arr));

    // 归并排序 https://segmentfault.com/a/1190000017833332
    function merge(left, right) {
      let temp = [];
      while (left.length && right.length) {
        if (left[0] < right[0]) {
          temp.push(left[0]);
          left.shift();
        } else {
          temp.push(right[0]);
          right.shift();
        }
      }

      return temp.concat(left, right);
    }

    function mergeSort(a) {
      if (a.length === 1) {
        return a;
      }
      let mid = Math.floor(a.length / 2);
      const left = a.slice(0, mid);
      const right = a.slice(mid);
      return merge(mergeSort(left), mergeSort(right));
    }

    // 快速排序
    // http://www.ruanyifeng.com/blog/2011/04/quicksort_in_javascript.html

    function quickSort(arr) {
      if (arr.length <= 1) {
        return arr;
      }
      var pivotIndex = Math.floor(arr.length / 2);
      var pivot = arr.splice(pivotIndex, 1)[0];
      var left = [];
      var right = [];
      for (var i = 0; i < arr.length; i++) {
        if (arr[i] < pivot) {
          left.push(arr[i]);
        } else {
          right.push(arr[i]);
        }
      }
      return quickSort(left).concat([pivot], quickSort(right));
    }

    console.log("qucik sort", quickSort(arr));
  </script>
</body>

</html>